View Javadoc

1   package net.sourceforge.simplegamenet.dice;
2   
3   import java.awt.*;
4   import java.awt.event.MouseEvent;
5   import java.awt.event.MouseListener;
6   import javax.swing.*;
7   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
8   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
9   
10  public class DiceScorePlayPanel extends JPanel implements MouseListener, DicePacketCodes {
11  
12      private DicePlayerClient dicePlayerClient;
13      private boolean myTurn = false;
14      private boolean allDisabled = false;
15      private DiceScoreComponent[][] diceScorePlayComponents = new DiceScoreComponent[16][8];
16      private JLabel[] playerNickLabels = {
17          new JLabel("1", (int) CENTER_ALIGNMENT),
18          new JLabel("2", (int) CENTER_ALIGNMENT),
19          new JLabel("3", (int) CENTER_ALIGNMENT),
20          new JLabel("4", (int) CENTER_ALIGNMENT),
21          new JLabel("5", (int) CENTER_ALIGNMENT),
22          new JLabel("6", (int) CENTER_ALIGNMENT),
23          new JLabel("7", (int) CENTER_ALIGNMENT),
24          new JLabel("8", (int) CENTER_ALIGNMENT),
25          new JLabel("1", (int) CENTER_ALIGNMENT),
26          new JLabel("2", (int) CENTER_ALIGNMENT),
27          new JLabel("3", (int) CENTER_ALIGNMENT),
28          new JLabel("4", (int) CENTER_ALIGNMENT),
29          new JLabel("5", (int) CENTER_ALIGNMENT),
30          new JLabel("6", (int) CENTER_ALIGNMENT),
31          new JLabel("7", (int) CENTER_ALIGNMENT),
32          new JLabel("8", (int) CENTER_ALIGNMENT)
33      };
34      private JLabel[] playLabels = {
35          new JLabel("Ones:"), new JLabel("Twos:"),
36          new JLabel("Threes:"), new JLabel("Fours:"),
37          new JLabel("Fives:"), new JLabel("Sixes:"),
38          new JLabel("Bonus (> 62):"), new JLabel("Left Total:"),
39          new JLabel("3 of a kind:"), new JLabel("4 of a kind:"),
40          new JLabel("Full house:"), new JLabel("Small street:"),
41          new JLabel("Big street:"),
42          new JLabel("Five of a kind:"), new JLabel("Chance:"),
43          new JLabel("Right Total:")
44      };
45      private JLabel[] playValueLabels = {
46          new JLabel("", (int) CENTER_ALIGNMENT),
47          new JLabel("", (int) CENTER_ALIGNMENT),
48          new JLabel("", (int) CENTER_ALIGNMENT),
49          new JLabel("", (int) CENTER_ALIGNMENT),
50          new JLabel("", (int) CENTER_ALIGNMENT)
51      };
52      // 0 bonusScore
53      // 1 fullHousScore
54      // 2 smallStreetScore
55      // 3 bigStreetScore
56      // 4 fOAKScore
57      private int[] settingValues = {0, 0, 0, 0, 0, 0, 0};
58      private Integer[] diceParticipantIDs;
59  
60      public DiceScorePlayPanel(DicePlayerClient dicePlayerClient) {
61          this.dicePlayerClient = dicePlayerClient;
62          ProportionLayout layout = new ProportionLayout();
63          layout.appendColumn(5);                                   // 0 empty
64          layout.appendColumn(0, ProportionLayout.NO_PROPORTION);   // 1 playLabels
65          layout.appendColumn(5);                                   // 2 empty
66          layout.appendColumn(20, ProportionLayout.NO_PROPORTION);   // 3 valueLabels
67          layout.appendColumn(10);                                  // 4 empty
68          for (int i = 0; i < 8; i++) {
69              layout.appendColumn(20, 1.0);
70          }                             // i + 5 leftPlayerComponents
71          layout.appendColumn(30);                                  // 13 empty
72          layout.appendColumn(0, ProportionLayout.NO_PROPORTION);   // 14 playLabels
73          layout.appendColumn(5);                                   // 15 empty
74          layout.appendColumn(20, ProportionLayout.NO_PROPORTION);   // 16 valueLabels
75          layout.appendColumn(10);                                  // 17 empty
76          for (int i = 0; i < 8; i++) {
77              layout.appendColumn(20, 1.0);
78          }                             // i + 18 rightPlayerComponents
79          layout.appendColumn(5);                                   // 26 empty
80          layout.appendRow(5, 1.0);                              // 0 empty
81          layout.appendRow(0, ProportionLayout.NO_PROPORTION);   // 1 nicks
82          layout.appendRow(5);                                   // 2 empty
83          for (int i = 0; i < 8; i++) {
84              layout.appendRow(20, 1.0);
85          }                             // i + 3 scoreComponents
86          layout.appendRow(5, 1.0);                              // 11 empty
87          setLayout(layout);
88          for (int i = 0; i < 16; i++) {
89              if (i < 8) {
90                  add(playLabels[i], new ProportionConstraints(1, (i + 3)));
91                  if (i == 6) {
92                      add(playValueLabels[0], new ProportionConstraints(3, (i + 3)));
93                  }
94              } else {
95                  add(playLabels[i], new ProportionConstraints(14, (i - 5)));
96                  if (i >= 10 && i <= 13) {
97                      add(playValueLabels[i - 9], new ProportionConstraints(16, (i - 5)));
98                  }
99              }
100         }
101         for (int i = 0; i < 16; i++) {
102             for (int j = 0; j < 8; j++) {
103                 diceScorePlayComponents[i][j] = new DiceScoreComponent(true);
104                 diceScorePlayComponents[i][j].addMouseListener(this);
105             }
106         }
107         for (int i = 0; i < 16; i++) {
108             if (i < 8) {
109                 for (int j = 0; j < 8; j++) {
110                     add(playerNickLabels[j], new ProportionConstraints(j + 5, 1));
111                     add(diceScorePlayComponents[i][j],
112                             new ProportionConstraints((i + 5), (j + 3)));
113                 }
114             } else {
115                 for (int j = 0; j < 8; j++) {
116                     add(playerNickLabels[j + 8], new ProportionConstraints(j + 18, 1));
117                     add(diceScorePlayComponents[i][j],
118                             new ProportionConstraints((i + 10), (j + 3)));
119                 }
120             }
121         }
122         for (int i = 0; i < playValueLabels.length; i++) {
123             playValueLabels[i].setText(new Integer(settingValues[i + 2]).toString());
124             playValueLabels[i].setBorder(BorderFactory.createEtchedBorder());
125         }
126         add(playValueLabels[0], new ProportionConstraints(3, 9));
127         for (int i = 1; i < playValueLabels.length; i++) {
128             add(playValueLabels[i], new ProportionConstraints(16, (i + 4)));
129         }
130         showScorePlayCases();
131     }
132 
133     public void showScorePlayCases() {
134         for (int i = 0; i < 16; i++) {
135             for (int j = 0; j < 8; j++) {
136                 diceScorePlayComponents[i][j].setBorder(BorderFactory.createEtchedBorder());
137                 diceScorePlayComponents[i][j].setEnabled(true);
138             }
139         }
140     }
141 
142     public void setScorePlayCases() {
143         diceParticipantIDs = dicePlayerClient.getDiceParticipantIDs();
144         int amountValues = dicePlayerClient.getDicePlayerAmount();
145         int[] settingValues = dicePlayerClient.getSettingValues();
146         boolean iChanger = true;
147         for (int i = 0; i < playValueLabels.length; i++) {
148             playValueLabels[i].setText(new Integer(settingValues[i + 2]).toString());
149             playValueLabels[i].setBorder(BorderFactory.createEtchedBorder());
150         }
151         for (int i = 0; i < 16; i++) {
152             for (int j = 0; j < 8; j++) {
153                 diceScorePlayComponents[i][j].setScoreLabel("");
154                 diceScorePlayComponents[i][j].setBorder(BorderFactory.createEtchedBorder());
155                 diceScorePlayComponents[i][j].setEnabled(true);
156             }
157         }
158         for (int i = amountValues; i < 16; i++) {
159             if (i < 8) {
160                 for (int j = 0; j < 8; j++) {
161                     diceScorePlayComponents[i][j].setBorder(
162                             BorderFactory.createLoweredBevelBorder());
163                     diceScorePlayComponents[i][j].setEnabled(false);
164                 }
165             } else {
166                 if (iChanger) {
167                     i = amountValues + 8;
168                     iChanger = false;
169                 }
170                 if (i >= 16) {
171                     break;
172                 } else {
173                     for (int j = 0; j < 8; j++) {
174                         diceScorePlayComponents[i][j].setBorder(
175                                 BorderFactory.createLoweredBevelBorder());
176                         diceScorePlayComponents[i][j].setEnabled(false);
177                     }
178                 }
179             }
180         }
181     }
182 
183     /***
184      * Invoked when the mouse button has been clicked (pressed and released) on a component.
185      */
186     public void mouseClicked(MouseEvent mouseEvent) {
187     }
188 
189     /***
190      * Invoked when the mouse enters a component.
191      */
192     public void mouseEntered(MouseEvent mouseEvent) {
193         if (myTurn) {
194             for (int i = 0; i < diceParticipantIDs.length; i++) {
195                 if (dicePlayerClient.getNextPlayer().equals(diceParticipantIDs[i])) {
196                     for (int j = 0; j < 7; j++) {
197                         if (mouseEvent.getSource() == diceScorePlayComponents[i][j]
198                                 && diceScorePlayComponents[i][j].isEnabled()) {
199                             if (j != 6) {
200                                 diceScorePlayComponents[i][j]
201                                         .setBorder(BorderFactory.createLineBorder(Color.RED));
202                             }
203                         } else if (mouseEvent.getSource() == diceScorePlayComponents[i + 8][j]
204                                 && diceScorePlayComponents[i + 8][j].isEnabled()) {
205                             diceScorePlayComponents[i + 8][j]
206                                     .setBorder(BorderFactory.createLineBorder(Color.RED));
207                         }
208                     }
209                 }
210             }
211         }
212     }
213 
214     /***
215      * Invoked when the mouse exits a component.
216      */
217     public void mouseExited(MouseEvent mouseEvent) {
218         if (myTurn) {
219             for (int i = 0; i < diceParticipantIDs.length; i++) {
220                 if (dicePlayerClient.getNextPlayer().equals(diceParticipantIDs[i])) {
221                     for (int j = 0; j < 7; j++) {
222                         if (mouseEvent.getSource() == diceScorePlayComponents[i][j]
223                                 && diceScorePlayComponents[i][j].isEnabled()) {
224                             if (j != 6) {
225                                 diceScorePlayComponents[i][j]
226                                         .setBorder(BorderFactory.createRaisedBevelBorder());
227                             }
228                         } else if (mouseEvent.getSource() == diceScorePlayComponents[i + 8][j]
229                                 && diceScorePlayComponents[i + 8][j].isEnabled()) {
230                             diceScorePlayComponents[i + 8][j]
231                                     .setBorder(BorderFactory.createRaisedBevelBorder());
232                         }
233                     }
234                 }
235             }
236         }
237     }
238 
239     /***
240      * Invoked when a mouse button has been pressed on a component.
241      */
242     public void mousePressed(MouseEvent mouseEvent) {
243         if (myTurn) {
244             for (int i = 0; i < diceParticipantIDs.length; i++) {
245                 if (dicePlayerClient.getNextPlayer().equals(diceParticipantIDs[i])) {
246                     for (int j = 0; j < 7; j++) {
247                         if (mouseEvent.getSource() == diceScorePlayComponents[i][j]
248                                 && diceScorePlayComponents[i][j].isEnabled()) {
249                             if (j != 6) {
250                                 diceScorePlayComponents[i][j]
251                                         .setBorder(BorderFactory.createLineBorder(Color.GREEN));
252                                 setMyTurn(false);
253                                 DicePacket dicePacket =
254                                         new DicePacket(TURN_FILL_SCORE, new Integer(j));
255                                 dicePlayerClient.sendDicePacket(dicePacket);
256                             }
257                         } else if (mouseEvent.getSource() == diceScorePlayComponents[i + 8][j]
258                                 && diceScorePlayComponents[i + 8][j].isEnabled()) {
259                             diceScorePlayComponents[i + 8][j]
260                                     .setBorder(BorderFactory.createLineBorder(Color.GREEN));
261                             setMyTurn(false);
262                             DicePacket dicePacket =
263                                     new DicePacket(TURN_FILL_SCORE, new Integer(j + 8));
264                             dicePlayerClient.sendDicePacket(dicePacket);
265                         }
266                     }
267                 }
268             }
269         }
270     }
271 
272     /***
273      * Invoked when a mouse button has been released on a component.
274      */
275     public void mouseReleased(MouseEvent mouseEvent) {
276     }
277 
278     public void setMyTurn(boolean myTurn) {
279         this.myTurn = myTurn;
280         for (int i = 0; i < diceParticipantIDs.length; i++) {
281             for (int j = 0; j < 8; j++) {
282                 if (diceScorePlayComponents[i][j].isEnabled()) {
283                     diceScorePlayComponents[i][j]
284                             .setBorder(BorderFactory.createEtchedBorder());
285                 } else {
286                     diceScorePlayComponents[i][j]
287                             .setBorder(BorderFactory.createEmptyBorder());
288                 }
289                 if (diceScorePlayComponents[i + 8][j].isEnabled()) {
290                     diceScorePlayComponents[i + 8][j]
291                             .setBorder(BorderFactory.createEtchedBorder());
292                 } else {
293                     diceScorePlayComponents[i + 8][j]
294                             .setBorder(BorderFactory.createEmptyBorder());
295                 }
296             }
297         }
298         if (myTurn) {
299             for (int i = 0; i < diceParticipantIDs.length; i++) {
300                 if (dicePlayerClient.getNextPlayer().equals(diceParticipantIDs[i])) {
301                     for (int j = 0; j < 7; j++) {
302                         if (diceScorePlayComponents[i][j].isEnabled()) {
303                             if (j < 6) {
304                                 diceScorePlayComponents[i][j]
305                                         .setBorder(BorderFactory.createRaisedBevelBorder());
306                             }
307                         }
308                         if (diceScorePlayComponents[i + 8][j].isEnabled()) {
309                             diceScorePlayComponents[i + 8][j]
310                                     .setBorder(BorderFactory.createRaisedBevelBorder());
311                         }
312                     }
313                 }
314             }
315         }
316     }
317 
318     public boolean checkAllDisabled(int playerIDIndex) {
319         for (int i = 0; i < 6; i++) {
320             if (diceScorePlayComponents[playerIDIndex][i].isEnabled()) {
321                 return false;
322             }
323         }
324         return true;
325     }
326 
327     public void fillScore(int[] filledScore) {
328         if (filledScore[1] < 8
329                 && diceScorePlayComponents[filledScore[0]][filledScore[1]].isEnabled()) {
330             diceScorePlayComponents[filledScore[0]][filledScore[1]]
331                     .setScoreLabel((new Integer(filledScore[2])).toString());
332         } else if (filledScore[1] >= 8) {
333             diceScorePlayComponents[filledScore[0] + 8][filledScore[1] - 8]
334                     .setScoreLabel((new Integer(filledScore[2])).toString());
335         }
336     }
337 
338     public void adjustSettingValue(int caseNumber, int settingValue) {
339         playValueLabels[caseNumber].setText(new Integer(settingValue).toString());
340     }
341 
342     public void fillTotals(int leftTotal, int rightTotal) {
343         for (int i = 0; i < diceParticipantIDs.length; i++) {
344             if (dicePlayerClient.getNextPlayer().equals(diceParticipantIDs[i])) {
345                 diceScorePlayComponents[i][7]
346                         .setScoreLabel((new Integer(leftTotal)).toString());
347                 diceScorePlayComponents[i + 8][7]
348                         .setScoreLabel((new Integer(rightTotal)).toString());
349             }
350         }
351     }
352 
353     public boolean checkEnabled(Integer diceParticipantID) {
354         for (int i = 0; i < diceParticipantIDs.length; i++) {
355             if (diceParticipantID.equals(diceParticipantIDs[i])) {
356                 return diceScorePlayComponents[i][6].isEnabled();
357             }
358         }
359         return false;
360     }
361 
362     public boolean gameOver() {
363         for (int i = 0; i < 16; i++) {
364             for (int j = 0; j < 8; j++) {
365                 if (diceScorePlayComponents[i][j].isEnabled()) {
366                     return false;
367                 }
368             }
369         }
370         return true;
371     }
372 }